package liumengbaio.lolzhangyoubao.Activity.Adapter;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import liumengbaio.lolzhangyoubao.Activity.InFo.PlayersPublishInfo;
import liumengbaio.lolzhangyoubao.R;

/**
 * Created by lanouhn on 16/9/10.
 */
public class PlayersPublishAdapter extends BaseAdapter {
    private PlayersPublishInfo playersPublishInfo;
    private Context context;


    public PlayersPublishAdapter(PlayersPublishInfo playersPublishInfo, Context context) {
        this.playersPublishInfo = playersPublishInfo;
        this.context = context;
    }

    String firstStr;
    String secondStr;
    String thirdStr;
    String fourthStr;
    String fifthStr;
    String sixthStr;

    AssetManager assets = null;



    @Override
    public int getCount() {
        return playersPublishInfo.getData().size();
    }

    @Override
    public Object getItem(int position) {
        return playersPublishInfo.getData().get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (viewHolder == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_playerspublish, null);
            viewHolder.tv_nickname = (TextView) convertView.findViewById(R.id.tv_nickname);
            viewHolder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
            viewHolder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
            viewHolder.tv_map = (TextView) convertView.findViewById(R.id.tv_map);
            viewHolder.area_summoner = (TextView) convertView.findViewById(R.id.area_summoner);
            viewHolder.tv_zdl = (TextView) convertView.findViewById(R.id.tv_zdl);
            viewHolder.tv_good = (TextView) convertView.findViewById(R.id.tv_good);
            viewHolder.tv_comment = (TextView) convertView.findViewById(R.id.tv_comment);
            viewHolder.iv_first = (ImageView) convertView.findViewById(R.id.iv_equipfirst);
            viewHolder.iv_second = (ImageView) convertView.findViewById(R.id.iv_equipsecond);
            viewHolder.iv_third = (ImageView) convertView.findViewById(R.id.iv_equipthird);
            viewHolder.iv_fourth = (ImageView) convertView.findViewById(R.id.iv_equipfourth);
            viewHolder.iv_fifth = (ImageView) convertView.findViewById(R.id.iv_equipfifth);
            viewHolder.iv_sixth = (ImageView) convertView.findViewById(R.id.iv_equipsixth);


            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        PlayersPublishInfo.DataBean bean = playersPublishInfo.getData().get(position);
        int date_published = bean.getCreated();
        long date = date_published * 1000L;
        Date d = new Date(date);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = format.format(d);
        viewHolder.tv_time.setText(dateStr);
        viewHolder.tv_nickname.setText("发布者:" + bean.getNickname());
        viewHolder.tv_title.setText(bean.getTitle());
        viewHolder.tv_map.setText(bean.getMap());
        viewHolder.area_summoner.setText(bean.getArea() + "-" + bean.getSummoner());
        viewHolder.tv_zdl.setText(bean.getZdl());
        viewHolder.tv_good.setText(bean.getGood());
        viewHolder.tv_comment.setText(bean.getComment());
        String str = bean.getEquips();
        firstStr = str.substring(0, 5);
        secondStr = str.substring(6, 11);
        thirdStr = str.substring(12, 17);
        fourthStr = str.substring(18, 23);
        fifthStr = str.substring(24, 29);
        sixthStr = str.substring(30, 35);

        assets = context.getAssets();

        InputStream assetFile = null;
        InputStream assetFileTwo = null;
        InputStream assetFileThere = null;
        InputStream assetFileFour = null;
        InputStream assetFileFive = null;
        InputStream assetFileSix = null;

        try {
            assetFile = assets.open("pic/" + firstStr + ".jpg");
            assetFileTwo = assets.open("pic/" + secondStr + ".jpg");
            assetFileThere = assets.open("pic/" + thirdStr + ".jpg");
            assetFileFour = assets.open("pic/" + fourthStr + ".jpg");
            assetFileFive = assets.open("pic/" + fifthStr + ".jpg");
            assetFileSix = assets.open("pic/" + sixthStr + ".jpg");

        } catch (IOException e) {
            e.printStackTrace();
        }
        BitmapDrawable bitmapDrawable = (BitmapDrawable) viewHolder.iv_first.getDrawable();
        if (bitmapDrawable != null && !bitmapDrawable.getBitmap().isRecycled()) {
            bitmapDrawable.getBitmap().recycle();
        }
        viewHolder.iv_first.setImageBitmap(BitmapFactory.decodeStream(assetFile));
        viewHolder.iv_second.setImageBitmap(BitmapFactory.decodeStream(assetFileTwo));
        viewHolder.iv_third.setImageBitmap(BitmapFactory.decodeStream(assetFileThere));
        viewHolder.iv_fourth.setImageBitmap(BitmapFactory.decodeStream(assetFileFour));
        viewHolder.iv_fifth.setImageBitmap(BitmapFactory.decodeStream(assetFileFive));
        viewHolder.iv_sixth.setImageBitmap(BitmapFactory.decodeStream(assetFileSix));



        return convertView;
    }

    private class ViewHolder {
        private TextView tv_nickname, tv_title, tv_time, tv_map, area_summoner, tv_zdl, tv_good, tv_comment;
        private ImageView iv_first, iv_second, iv_third, iv_fourth, iv_fifth, iv_sixth;
    }


}
